﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Security.Cryptography;
using System.Text;

namespace Bus_proj.website
{
    public partial class Detail_user : System.Web.UI.Page
    {
        int point_distance, promotion_distance, sum_distance;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 1)
                    {
                        int user = Int32.Parse(Session["login_User"].ToString());
                        string user_detail = @"select *
                                from Table_Passenger                                    
                                inner join District
                                    on Table_Passenger.Passenger_district = District.DistrictId
                                inner join Amphur
                                    on Table_Passenger.Passenger_sub_district = Amphur.AmphurId
                                inner join Province
	                                on Table_Passenger.Passenger_province = Province.ProvinceId
                                where Passenger_id = '" + user + "'";
                        string distance = @"select Point_distance
                                from Table_Sale
                                inner join Table_Around
                                    on Table_Around.Around_id = Table_Sale.Around_id
                                inner join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id
                                inner join Table_Passenger
                                    on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                                inner join Table_Point
                                    on Table_Sale.Point_id = Table_Point.Point_id                                
                                where Table_Passenger.Passenger_id = '" + user + "' and Table_Sale.SSID = 1";
                        string promotion_item = @"select Table_promotion_item.promotion_item_distance
                                    from Table_Passenger
                                    inner join Table_store_user
                                    on Table_Passenger.Passenger_id = Table_store_user.Passenger_id
                                    inner join Table_promotion_item
                                    on Table_promotion_item.promotion_item_id = Table_store_user.promotion_item_id
                                    where Table_Passenger.Passenger_id = '" + user + "' and Table_promotion_item.promotion_item_status = 1";
                        SqlCommand sqlcomm = new SqlCommand();
                        string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                        SqlConnection sqlconn = new SqlConnection(Connection);
                        SqlDataAdapter da = new SqlDataAdapter(user_detail, sqlconn);
                        SqlDataAdapter da2 = new SqlDataAdapter(distance, sqlconn);
                        SqlDataAdapter da_P = new SqlDataAdapter(promotion_item, sqlconn);
                        DataTable dt = new DataTable();
                        DataTable dt2 = new DataTable();
                        DataTable dt_P = new DataTable();
                        da.Fill(dt);
                        da2.Fill(dt2);
                        da_P.Fill(dt_P);
                        try
                        {
                            TextBox18.Text = dt.Rows[0]["Passenger_id"].ToString();
                            TextBox13.Text = dt.Rows[0]["Passenger_prefix"].ToString();
                            TextBox1.Text = dt.Rows[0]["Passenger_name"].ToString();
                            TextBox2.Text = dt.Rows[0]["Passenger_surname"].ToString();
                            TextBox14.Text = dt.Rows[0]["Passenger_sex"].ToString();
                            TextBox4.Text = dt.Rows[0]["Passenger_birthday"].ToString();
                            TextBox5.Text = dt.Rows[0]["Passenger_home_no"].ToString();
                            TextBox6.Text = dt.Rows[0]["Passenger_village_no"].ToString();
                            TextBox7.Text = dt.Rows[0]["Passenger_lane"].ToString();
                            TextBox12.Text = dt.Rows[0]["Passenger_road"].ToString();
                            TextBox17.Text = dt.Rows[0]["DistrictName"].ToString();
                            TextBox16.Text = dt.Rows[0]["AmphurName"].ToString();
                            TextBox15.Text = dt.Rows[0]["ProvinceName"].ToString();
                            TextBox8.Text = dt.Rows[0]["Passenger_post"].ToString();
                            TextBox9.Text = dt.Rows[0]["Passenger_mail"].ToString();
                            TextBox11.Text = dt.Rows[0]["Passenger_tel"].ToString();
                            TextBox3.Text = dt.Rows[0]["Passenger_identification"].ToString();
                            if (dt2.Rows.Count <= 0)
                            {
                                point_distance = 0;
                            }
                            else
                            {
                                for (int i = 0; i < dt2.Rows.Count; i++)
                                {
                                    point_distance += Int32.Parse(dt2.Rows[i]["Point_distance"].ToString());
                                }
                            }
                            if (dt_P.Rows.Count <= 0)
                            {
                                promotion_distance = 0;
                            }
                            else
                            {
                                for (int i = 0; i < dt_P.Rows.Count; i++)
                                {
                                    promotion_distance += Int32.Parse(dt_P.Rows[i]["promotion_item_distance"].ToString());
                                }
                            }
                            sum_distance = point_distance - promotion_distance;
                            TextBox19.Text = point_distance.ToString();
                            TextBox20.Text = sum_distance.ToString();
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }                            
        }
        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox21.Text.Equals(TextBox22.Text))
                {
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    string pass = TextBox10.Text;
                    string identication = Session["login_Passenger_identification"].ToString();
                    var q = from p in db.Table_Passengers
                            where p.Passenger_identification == identication & p.password == getMd5Hash(pass)
                            select p;
                    q.ToList().ForEach(c => c.password = getMd5Hash(TextBox21.Text));
                    db.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เปลี่ยนรหัสผ่านเสร็จสิ้น');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเปลี่ยนรหัสผ่านได้');", true);
                }

            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเปลี่ยนรหัสผ่านได้');", true);
            }
        }
    }
}